package com.example.administrator.ygddriver.into.register.forget;

import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.widget.CardView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import com.example.administrator.ygddriver.R;
import com.example.administrator.ygddriver.base.BaseActivity;
import com.example.administrator.ygddriver.base.BaseBean;
import com.example.administrator.ygddriver.base.BaseHint;
import com.example.administrator.ygddriver.into.register.forget.reset.ResetActivity;
import com.example.administrator.ygddriver.tool.IsPhoneNum;
import com.example.administrator.ygddriver.tool.OkGoRequests;
import com.example.administrator.ygddriver.tool.SimpleToolbar;
import com.example.administrator.ygddriver.tool.UrlValuse;

import java.util.HashMap;

/**
 * 忘记密码界面
 */
public class ForGetActivity extends BaseActivity implements View.OnClickListener {
    private ImageView mPhoneDelIv;
    private EditText mPhoneEt, mVerificationEt;
    private SimpleToolbar simpleToolbar;
    private CardView mNextCardView;
    private Button mGetVerificationBtn;
    private CountDownTimer timer;

    @Override
    public int initLayout() {
        return R.layout.activity_forget;
    }

    @Override
    public void initView() {
        mPhoneDelIv = findViewById(R.id.activity_forget_account_del_iv);
        mPhoneEt = findViewById(R.id.activity_forget_phone_num_et);
        simpleToolbar = findViewById(R.id.activity_forget_stb);
        mNextCardView = findViewById(R.id.activity_forget_next_cv);
        mGetVerificationBtn = findViewById(R.id.activity_forget_get_verification_btn);
        mVerificationEt = findViewById(R.id.activity_forget_verification_et);
        mGetVerificationBtn.setOnClickListener(this);
        mNextCardView.setOnClickListener(this);
        mPhoneDelIv.setOnClickListener(this);
    }

    @Override
    public void initDate() {
        setSimpleToolbar();
        addEt();
    }

    private void setSimpleToolbar() {
        simpleToolbar.setMainTitle("忘记密码");
        simpleToolbar.setMainTitleColor("#000000");
        simpleToolbar.setBackIv(R.mipmap.u126);
        simpleToolbar.setToolColor("#ffffff");
        simpleToolbar.setBackIvClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void addEt() {
        mPhoneEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mPhoneDelIv.setVisibility(View.VISIBLE);
                if (TextUtils.isEmpty(mPhoneEt.getText())) {
                    mPhoneDelIv.setVisibility(View.GONE);
                } else {
                    mPhoneDelIv.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mPhoneEt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    if (TextUtils.isEmpty(mPhoneEt.getText())) {
                        mPhoneDelIv.setVisibility(View.GONE);
                    } else {
                        mPhoneDelIv.setVisibility(View.VISIBLE);
                    }
                } else {
                    mPhoneDelIv.setVisibility(View.GONE);
                }
            }
        });


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_forget_next_cv:
                if (judge(mPhoneEt.getText().toString(), mVerificationEt.getText().toString())) {
                    initcheckForgetPwdAuthCodeHtml(mPhoneEt.getText().toString(), mVerificationEt.getText().toString());
                }

                break;
            case R.id.activity_forget_account_del_iv:
                mPhoneEt.setText("");
                break;
            case R.id.activity_forget_get_verification_btn:
                startTime();
                if (IsPhoneNum.isPhoneNumber(String.valueOf(mPhoneEt.getText()))) {
                    HashMap<String, String> params = new HashMap<>();
                    params.put("c_phone", String.valueOf(mPhoneEt.getText()));
                    params.put("type", "1");
                    initGetVerificationHtml(params);
                } else {
                    timer.cancel();
                    BaseHint baseHint = new BaseHint.addHint().setParameter(this, "提示", "请输入正确电话号码");
                    baseHint.show();
                }
                break;
        }
    }

    private Boolean judge(String account, String code) {
        if (TextUtils.isEmpty(account) || TextUtils.isEmpty(code)) {
            BaseHint baseHint = new BaseHint.addHint().setParameter(ForGetActivity.this, "提示", "信息不可为空");
            baseHint.show();
            return false;
        }
        if (!IsPhoneNum.isPhoneNumber(account)) {
            BaseHint baseHint = new BaseHint.addHint().setParameter(ForGetActivity.this, "提示", "请输入正确的手机账号");
            baseHint.show();
            return false;
        }
        if (code.length() < 6) {
            BaseHint baseHint = new BaseHint.addHint().setParameter(ForGetActivity.this, "提示", "验证码错误");
            baseHint.show();
            return false;
        }

        return true;
    }

    //获取验证码
    private void initGetVerificationHtml(HashMap<String, String> params) {
        OkGoRequests.getInstance().putRequestAsync(UrlValuse.POST_API_BASE + UrlValuse.CREATE_VERIFICATION_CODE, params, BaseBean.class, new OkGoRequests.OnTrue<BaseBean>() {
            @Override
            public void hasData(BaseBean data) {
                if (data.isSuccess()) {

                } else {
                    timer.cancel();
                    mGetVerificationBtn.setClickable(true);
                    mGetVerificationBtn.setText("获取验证码");
                    BaseHint baseHint = new BaseHint.addHint().setParameter(ForGetActivity.this, "提示", data.getLocalizedMessage());
                    baseHint.show();
                }

            }
        }, new OkGoRequests.OnError<BaseBean>() {
            @Override
            public void noData() {

            }
        });


    }

    private void startTime() {
        timer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mGetVerificationBtn.setClickable(false);
                mGetVerificationBtn.setText((millisUntilFinished / 1000) + "秒");
            }

            @Override
            public void onFinish() {
                mGetVerificationBtn.setClickable(true);
                mGetVerificationBtn.setText("获取验证码");
            }
        }.start();


    }

    //忘记密码 验证手机号及验证码
    private void initcheckForgetPwdAuthCodeHtml(final String phone, String code) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("c_account", phone);
        hashMap.put("c_code", code);
        hashMap.put("c_type", "1");
        OkGoRequests.getInstance().putRequestAsync(UrlValuse.POST_API_BASE+UrlValuse.CHECK_FORGET_PW_AUTHCODE, hashMap, ForGetRequestBean.class, new OkGoRequests.OnTrue<ForGetRequestBean>() {
            @Override
            public void hasData(ForGetRequestBean date) {
                if (date.isSuccess()) {
                    Intent intent = new Intent(ForGetActivity.this, ResetActivity.class);
                    intent.putExtra("phone", phone);
                    startActivity(intent);
                    finish();
                } else {
                    BaseHint baseHint = new BaseHint.addHint().setParameter(ForGetActivity.this, "提示", date.getLocalizedMessage());
                    baseHint.show();
                }

            }
        }, new OkGoRequests.OnError<ForGetRequestBean>() {
            @Override
            public void noData() {

            }
        });

    }
}
